﻿Imports SFDL.LoaderExtensions
Imports SFDL.Core

Module SpeedreportHelper

    Private _log As NLog.Logger = NLog.LogManager.GetLogger("SpeedreportHelper")

    Private Function CalculateSizeAsMB(ByVal _item_list As List(Of OLV_Item)) As Double

        Dim _full_session_size As Double

        _full_session_size = _item_list.Aggregate(_full_session_size, Function(current, _file) current + _file.SizeDownloaded)

        _full_session_size = (_full_session_size / 1024) / 1024

        Return _full_session_size

    End Function

    Private Function CalculateSpeed(ByVal _starttime As Date, ByVal _stoptime As Date, ByVal _item_list As List(Of OLV_Item)) As Double

        Dim _full_session_size As Double
        Dim _time_elapsed As Double
        Dim _rt As Double

        _full_session_size = _item_list.Aggregate(_full_session_size, Function(current, _file) current + _file.SizeDownloaded)

        _full_session_size = _full_session_size / 1024

        _time_elapsed = DateDiff(DateInterval.Second, _starttime, _stoptime)

        Debug.WriteLine(String.Format("{0} KB in {1} Sekunden heruntergeladen", _full_session_size, _time_elapsed))

        _rt = _full_session_size / _time_elapsed

        Return _rt

    End Function

    Public Function GenerateSpeedreport(ByVal SFDLSession As SFDLSession) As String

        Dim _tools As New Tools
        Dim _speed As Double
        Dim _size As Double
        Dim _tmp_list_check_items As New List(Of OLV_Item)
        Dim _speedreport_settings As New SpeedreportSettings
        Dim _speedreport_text As String = String.Empty

        Try

            _speedreport_settings = GetSettings.SpeedReportSettings

            _tmp_list_check_items.AddRange(From _item In SFDLSession.OLVItemList Where _item.Status = Status.Complete Or _item.Status = Status.CompleteHashInvalid Or _item.Status = Status.CompleteHashValid)

            _speed = CalculateSpeed(SFDLSession.TimeDownloadStarted, SFDLSession.TimeDownloadStopped, _tmp_list_check_items)
            _size = CalculateSizeAsMB(_tmp_list_check_items)


            _speedreport_text = _speedreport_settings.SpeedreportTemplate


            'Variablen ersetzten
            _speedreport_text = _speedreport_text.Replace("%%USER%%", _speedreport_settings.SpeedreportUsername)
            _speedreport_text = _speedreport_text.Replace("%%CONNECTION%%", _speedreport_settings.SpeedreportConnection)
            _speedreport_text = _speedreport_text.Replace("%%COMMENT%%", _speedreport_settings.SpeedreportComment)
            _speedreport_text = _speedreport_text.Replace("%%SPEED%%", Math.Round(_speed, 2) & " KB/s")
            _speedreport_text = _speedreport_text.Replace("%%SFDL_DESC%%", SFDLSession.SFDLFile.Description)
            _speedreport_text = _speedreport_text.Replace("%%SFDL_UPPER%%", SFDLSession.SFDLFile.Uploader)
            _speedreport_text = _speedreport_text.Replace("%%TIME%%", _tools.SecToHMS(DateDiff(DateInterval.Second, SFDLSession.TimeDownloadStarted, SFDLSession.TimeDownloadStopped)))
            _speedreport_text = _speedreport_text.Replace("%%SFDL_SIZE%%", Math.Round(_size, 2) & " MB")

        Catch ex As Exception
            _log.Error(ex, ex.Message)
        End Try

        Return _speedreport_text

    End Function


End Module
